<?php
namespace Components\User\Sha;

class Auth {
  public static function encryptToken(array $payload) {
    return \Phalcon\Di::getDefault() -> getShared('crypt') -> encryptBase64(json_encode($payload), null, true);
  }

  public static function decryptToken($token) {
    return json_decode(\Phalcon\Di::getDefault() -> getShared('crypt') -> decryptBase64($token, null, true), true);
  }
  
  public static function checkToken($token) {
		if(is_null($token) || empty($token)) {return ['code'=>-1,'msg'=>'token未设置或错误,请重新登陆'];}
    $payload = self::decryptToken($token);
    if(!$payload) {return ['code'=>-1,'msg'=>'token未设置或错误,请重新登陆'];}

    if(isset($payload['exp']) && $payload['exp'] < time()) {return ['code'=>-1,'msg'=>'token已超时,请重新登陆'];}

		return ['code'=>0,'msg'=>'验证成功','payload'=>$payload];
  }

  public static function getLoginPassword($password) {
    return \Phalcon\Di::getDefault() -> getShared('security') -> hash($password);
  }

  public static function geSafePassword($password) {;
    return \Phalcon\Di::getDefault() -> getShared('security') -> hash($password);
  }
}
